package com.yssoft.service.core;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.yssoft.entity.core.TDdDetail;
import com.yssoft.mapper.core.TDdDetailMapper;
import com.yssoft.utils.LayuiUtil;
import com.yssoft.utils.UserHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author 陈翼东
 * @说明 字典
 * @time 2018/4/27 上午 11:25
 */
@Service
public class DdDetailService {
    
    @Resource
    private TDdDetailMapper ddDetailMapper;
    /**
     * 分页查询一级字典信息
     * @param page
     * @param limit
     * @return
     */
    public JSONObject queryDdDetailPage(int page, int limit, String described,String classify) {
        Integer companyId = UserHelper.getCurrentCompanyId();//当前登陆人企业
        PageHelper.startPage(page,limit);
        Map<String,Object> params=new HashMap<>();
        params.put("companyId" , companyId);
        params.put("described",described);
        params.put("classify",classify);
        List<TDdDetail> pagelist = ddDetailMapper.queryDdDetailPage(params);
        int pageCount = ddDetailMapper.queryDdDetailCount(params);
        try {
            return LayuiUtil.getJSONObjectByObject(pagelist,pageCount,"","");
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 查询字典信息
     * @param ddDetailId
     * @return
     */
    public TDdDetail queryDdDetailById(Integer ddDetailId) {
        return ddDetailMapper.selectByPrimaryKey(ddDetailId);
    }
    
    /**
     * 修改字典信息
     * @param ddDetail
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateDdDetail(TDdDetail ddDetail,String id) {
        ddDetail.setId(Integer.parseInt(id));
        ddDetail.settEId(UserHelper.getCurrentCompanyId());
        ddDetailMapper.updateByPrimaryKeySelective(ddDetail);
    }
    
    /**
     * 添加字典信息
     * @param ddDetail
     */
    @Transactional(rollbackFor = Exception.class)
    public void addDdDetail(TDdDetail ddDetail) {
        ddDetail.setDeleteFlag("0");
        ddDetail.settEId(UserHelper.getCurrentCompanyId());
        ddDetail.setCreateTime(new Date());
        ddDetail.setCreatorId(UserHelper.getCurrentUserId());
        ddDetail.setCreatorName(UserHelper.getCurrentUser().getPersonName());
        ddDetailMapper.insert(ddDetail);
    }
    
    /**
     * 删除多条字典信息
     * @param ids
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int delDdDetails(String ids) {
        String[] idArray=ids.split(",");
        List<String> idList = Arrays.asList(idArray);
       return ddDetailMapper.updateByIds(idList);
        
    }
    
    /**
     * 删除多条字典信息
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public int delDdDetail(String id) {
        TDdDetail ddDetail=new TDdDetail();
        ddDetail.setId(Integer.parseInt(id));
        ddDetail.setDeleteFlag("1");
       return ddDetailMapper.updateByPrimaryKeySelective(ddDetail);
       
    }
    
    /**
     * 查询当前企业的物料管理tree
     * @return
     */
    public Object queryMaterialDdDetailTree() {
        Integer companyId = UserHelper.getCurrentCompanyId();
       //原料类别 //rawClassify
       //包材类别 //packagingClassify
       //辅料类别 //accessoriesClassify
       //添加剂类别 //additiveClassify
        List<String> classifys=new ArrayList<>();
        classifys.add("rawClassify");
        classifys.add("packagingClassify");
        classifys.add("accessoriesClassify");
        classifys.add("additiveClassify");
        Map<String,Object> param=new HashMap<>();
        param.put("list",classifys);
        param.put("companyId",companyId);
        List<Map<String,Object>> material = ddDetailMapper.queryMyDdDetailByClassifys(param);
        List<Map<String,Object>> jsonArray=new ArrayList<>();
        
        Map<String,Object> rawClassifyMap=ddDetailMapper.selectByClassify("rawClassify");
        rawClassifyMap.put("children",new ArrayList<Map<String,Object>>());
        
        Map<String,Object> packagingClassifyMap=ddDetailMapper.selectByClassify("packagingClassify");

        packagingClassifyMap.put("children",new ArrayList<Map<String,Object>>());
        
        Map<String,Object> accessoriesClassifyMap=ddDetailMapper.selectByClassify("accessoriesClassify");
        accessoriesClassifyMap.put("children",new ArrayList<Map<String,Object>>());
        
        Map<String,Object> additiveClassifyMap=ddDetailMapper.selectByClassify("additiveClassify");
        additiveClassifyMap.put("children",new ArrayList<Map<String,Object>>());
    
        for (Map<String,Object> m : material) {
            switch (m.get("pid")+""){
                case "1":
                    List<Map<String,Object>> ch1= (List<Map<String,Object>>)rawClassifyMap.get("children");
                    ch1.add(m);
                    break;
                case "2":
                    List<Map<String,Object>> ch2= (List<Map<String,Object>>)packagingClassifyMap.get("children");
                    ch2.add(m);
                    break;
                case "3":
                    List<Map<String,Object>> ch3= (List<Map<String,Object>>)accessoriesClassifyMap.get("children");
                    ch3.add(m);
                    break;
                case "4":
                    List<Map<String,Object>> ch4= (List<Map<String,Object>>)additiveClassifyMap.get("children");
                    ch4.add(m);
                    break;
            }
        }
    
        jsonArray.add(rawClassifyMap);
        jsonArray.add(packagingClassifyMap);
        jsonArray.add(accessoriesClassifyMap);
        jsonArray.add(additiveClassifyMap);
        return JSONArray.toJSON(jsonArray);
    }
    
    
    /**
     * 查询当前企业的物料管理tree只有第一层
     * @return
     */
    public Object queryMaterialDdDetailTreeNoChilder() {
        Integer companyId = UserHelper.getCurrentCompanyId();
        //原料类别 //rawClassify
        //包材类别 //packagingClassify
        //辅料类别 //accessoriesClassify
        //添加剂类别 //additiveClassify
        List<String> classifys=new ArrayList<>();
        classifys.add("rawClassify");
        classifys.add("packagingClassify");
        classifys.add("accessoriesClassify");
        classifys.add("additiveClassify");
        Map<String,Object> param=new HashMap<>();
        param.put("list",classifys);
        param.put("companyId",companyId);
        List<Map<String,Object>> jsonArray=new ArrayList<>();
        Map<String,Object> rawClassifyMap=ddDetailMapper.selectByClassify("rawClassify");
        Map<String,Object> packagingClassifyMap=ddDetailMapper.selectByClassify("packagingClassify");
        Map<String,Object> accessoriesClassifyMap=ddDetailMapper.selectByClassify("accessoriesClassify");
        Map<String,Object> additiveClassifyMap=ddDetailMapper.selectByClassify("additiveClassify");
        
        jsonArray.add(rawClassifyMap);
        jsonArray.add(packagingClassifyMap);
        jsonArray.add(accessoriesClassifyMap);
        jsonArray.add(additiveClassifyMap);
        return JSONArray.toJSON(jsonArray);
    }
    
    
    /**
     * 查询当前企业的物料管理tree
     * @return
     */
    public Object queryMaterialDdDetailTableTree() {
        Integer companyId = UserHelper.getCurrentCompanyId();
        List<String> classifys=new ArrayList<>();
        classifys.add("rawClassify");
        classifys.add("packagingClassify");
        classifys.add("accessoriesClassify");
        classifys.add("additiveClassify");
        Map<String,Object> param=new HashMap<>();
        param.put("list",classifys);
        param.put("companyId",companyId);
        List<Map<String,Object>> material = ddDetailMapper.queryMyDdDetailByClassifys(param);
        List<Map<String,Object>> jsonArray=new ArrayList<>();
        //四个固定的类别
        Map<String,Object> rawClassifyMap=ddDetailMapper.selectByClassify("rawClassify");
        rawClassifyMap.put("pid","0");
        rawClassifyMap.put("isButton","0");
        Map<String,Object> packagingClassifyMap=ddDetailMapper.selectByClassify("packagingClassify");
        packagingClassifyMap.put("pid","0");
        packagingClassifyMap.put("isButton","0");
        Map<String,Object> accessoriesClassifyMap=ddDetailMapper.selectByClassify("accessoriesClassify");
        accessoriesClassifyMap.put("pid","0");
        accessoriesClassifyMap.put("isButton","0");
        Map<String,Object> additiveClassifyMap=ddDetailMapper.selectByClassify("additiveClassify");
        additiveClassifyMap.put("pid","0");
        additiveClassifyMap.put("isButton","0");
        jsonArray.add(rawClassifyMap);
        jsonArray.add(packagingClassifyMap);
        jsonArray.add(accessoriesClassifyMap);
        jsonArray.add(additiveClassifyMap);
        jsonArray.addAll(material);
        return JSONArray.toJSON(jsonArray);
    }
    
    /**
     * 递归删除字典信息(删除父级的同时删除子集)
     * @param id
     * @return
     */
    public Integer deleteRecursiveDdDetail(Integer id) {
        List<TDdDetail>  detailList=ddDetailMapper.queryChildrenDdDetail(id);
        List<String> idList=new ArrayList<>();
        for (TDdDetail ddDetail : detailList) {
            idList.add(ddDetail.getId()+"");
        }
       return  ddDetailMapper.updateByIds(idList);
    }
    
    /**
	 * @功能描述:递归查询字典树形数据
	 * @作者:戈超
	 * @return
	 */
	public List<Map<String, Object>> queryDDTree(Map<String,Object> param){
		/*企业类型待添加  param.put("companyId", ??);*/
		//获取区域一级节点
		List<Map<String, Object>> node = ddDetailMapper.queryTreeOneNode(param);
		for(Map<String, Object> dd : node){
			param.put("pId", dd.get("id"));
			List<Map<String, Object>> children = queryTreeNode(param);
			if(children.size() > 0){
				dd.put("children", children);
			}
		}
		return node;
	}
	
	/**
	 * @功能描述:递归查询一级区域下树形数据
	 * @作者:戈超
	 * @param param
	 * @return
	 */
	public List<Map<String, Object>> queryTreeNode(Map<String,Object> param){
		List<Map<String, Object>> treeNode = ddDetailMapper.queryTreeNode(param);
		for(Map<String, Object> dd : treeNode){
			param.put("pId", dd.get("id"));
			List<Map<String, Object>> children = queryTreeNode(param);
			if(children.size() > 0){
				dd.put("children", children);
			}
		}
		return treeNode;
	}
	/**
	 * APP获取数据字典
	 * @author 王永宁
	 */
	public List<TDdDetail> findAllByApp() {
		return ddDetailMapper.findAllByApp();
	}
    
    public List<TDdDetail> findDdDetailByParam(Map<String,Object> titleParam4) {
	   return  ddDetailMapper.selectDdDetailByParam(titleParam4);
    }
}
